<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest as BaseFormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\ValidationException;

class FormRequest extends BaseFormRequest
{
    public function authorize()
    {
        //false代表权限验证不通过，返回403错误
        //true代表权限认证通过
        return true;
    }

    // 重写ajax请求验证错误响应码（422 -> 400）
    protected function failedValidation(Validator $validator)
    {
        $validationException = new ValidationException($validator);
        $validationException->status = 400;
        throw ($validationException)
            ->errorBag($this->errorBag)
            ->redirectTo($this->getRedirectUrl());
    }
}
